$(()=>{
    if(sessionStorage.getItem("msg_data")==null){
        alert("您的登录信息已过期请重新登陆");
        window.location.href="http://172.18.80.9:8087/admin/login/login"
    }
    var msg_data =JSON.parse(sessionStorage.getItem("msg_data"));
    console.log(msg_data.id);
    console.log(msg_data.token);
    //   change改变的是否有上级单位承担;
    var typee=1;
    $('input:radio[name="item"]').change(function() {
        if ($(this).val()==1) {
            $("#zi").css("display","none");
            typee = $(this).val();
        } else {
            $("#zi").css("display","block");
            typee = $(this).val();
        }
    });
    //      查看detail
    var loc=location.href;
    var n1=loc.length;//地址的总长度
    var n2=loc.indexOf("id=");//取得=号的位置
    if(n2!=-1){
        var eid=decodeURI(loc.substr(n2+3, n1-n2));
        $.ajax({
            type:"POST",
            data:{id:eid,uid:msg_data.id,token:msg_data.token,status:2},
            dataType:"json",
            url:"http://172.18.80.9:8087/admin/principal/detail",
            success:function(data){
                console.log(data);
                if(data.code==9){
                    alert(data.msg)
                    window.location.href="http://172.18.80.9:8087/admin/login/login"
                    return false;
                }
                var num = data.data.num;
                $("#task").val(data.data.item_no);//课题号
                $("#person").val(data.data.project_principal)//项目负责人
                $("#projecttype").val(data.data.project_type)//项目类型
                $("#projectname1").val(data.data.project_name)//项目总名称
                // $("#projectname2").val(data.data.child_name)//项目子名称
                // if(data.data.child_name!=""){
                //     $("#item2").attr("checked","checked")
                //     $("#zi").css("display","block");
                // }
                //开始时间
                var str=data.data.begin_time;
                if(str ==null){
                    console.log("开始时间为null");
                }else{
                    arr=str.split(" ");
                    var begin_time = arr[0];
                }
                $("#begindate").val(begin_time)
                //结束时间
                var str=data.data.end_time;
                if(str==null){
                    console.log("结束时间为null");
                }else {
                    arr=str.split(" ");
                    var end_time = arr[0];
                }
                $("#enddate").val(end_time)
                //预算金额
                $("#budgetprice").val(data.data.total_money)
                //所属研究单位
                $("#company").val(data.data.belong_company)
                //发放方式
                $(".way>option:first-child").css("display","none")
                var fw=`<option value="${data.data.grant_way}">${data.data.way}</option>`
                $(".way").html(fw)
                //专利和论文预期数量
                var thesisnum =[];
                var patentnum=[];
                for(var d  of num){
                    if(d.type==1){
                        thesisnum.push(d)

                    }else if(d.type==2){
                        patentnum.push(d)
                    }

                }
                //论文的select渲染********************************************************************
                var p=`<tr  style="height: 120px;" class="eight" id = "b0">
                  <td>
                      <div>
                          <div style="font-size: 18px;color: #000;">论文预期数量 </div>
                          <div style="height: 2px;"></div>
                          <div style="display: flex;justify-content: left;">
                              <div style="font-size: 14px;color: #000;line-height: 30px;">论文类型</div>
                              <div style="width: 1%"></div>
                              <select style="width: 150px;height: 30px;" class="papertype">
                                <option value="${thesisnum[0].type_id}"  selected = "selected" style="display: none">${thesisnum[0].content}</option>
                              </select>
                              <div style="width: 143px"></div>
                              <span>&nbsp;</span><div style="font-size: 14px;color: #000;line-height: 30px;">预期数量</div>
                             <div style="width: 10px;"></div>
                              <input type="text" placeholder="数量" style="width: 150px;height: 30px;padding-left: 10px;" class="papernum" value="${thesisnum[0].type_count}">
                              <div style="width: 30px;height: 30px;border: 1px solid #999;line-height: 30px;text-align: center;border-left: none;color: #000;">篇</div>
                               <div style="width: 10px;"></div>
                      
                          </div>
                      </div>
                  </td>
              </tr>`;
                //循环剩下的数据
                var bIndex=0;
                for(var t=1;t<thesisnum.length;t++ ){
                    bIndex++;
                    p+=`<tr  style="height: 120px;" class="eight" id = ${'b'+bIndex}>
                  <td>
                      <div>
                          <div style="font-size: 18px;color: #000;">论文预期数量 </div>
                          <div style="height: 2px;"></div>
                          <div style="display: flex;justify-content: left;">
                              <div style="font-size: 14px;color: #000;line-height: 30px;">论文类型</div>
                              <div style="width: 1%"></div>
                              <select style="width: 150px;height: 30px;" class="papertype">
                          
                                <option value="${thesisnum[t].type_id}"  selected = "selected" style="display: none">${thesisnum[t].content}</option>
                              </select>
                              <div style="width: 143px"></div>
                              <span>&nbsp;</span><div style="font-size: 14px;color: #000;line-height: 30px;">预期数量</div>
                             <div style="width: 10px;"></div>
                              <input type="text" placeholder="数量" style="width: 150px;height: 30px;padding-left: 10px;" class="papernum" value="${thesisnum[t].type_count}">
                              <div style="width: 30px;height: 30px;border: 1px solid #999;line-height: 30px;text-align: center;border-left: none;color: #000;">篇</div>
                               <div style="width: 10px;"></div>
                           
                          </div>
                      </div>
                  </td>
              </tr>`
                }
                $("#t1").html(p);
                //专利的select渲染********************************************************************
                var c=`<tr style="height: 120px;" id="a1">
                            <td>
                                <div>
                                    <div style="font-size: 18px;color: #000;">专利预期数量 </div>
                                    <div style="height: 2px;"></div>
                                    <div style="display: flex;justify-content: left;">
                                        <div style="font-size: 14px;color: #000;line-height: 30px;">专利类型</div>
                                        <div style="width: 1%"></div>
                                        <select style="width: 150px;height: 30px;"class="patentname">
                                              <option value="${patentnum[0].type_id}"  selected = "selected" style="display: none">${patentnum[0].content}</option>       
                                        </select>
                                        <div style="width: 143px"></div>
                                        <span>&nbsp;</span><div style="font-size: 14px;color: #000;line-height: 30px;">预期数量</div>
                                        <div style="width: 10px;"></div>
                                        <input type="text" placeholder="数量" style="width: 150px;height: 30px;padding-left: 10px;margin-top: 0;" class="patentnum" value="${patentnum[0].type_count}">
                                        <div style="width: 30px;height: 30px;border: 1px solid #999;line-height: 30px;text-align: center;border-left: none;color: #000;">项</div>
                                        <div style="width: 10px;"></div>
                                 
                                    </div>
                                </div>
                            </td>
                        </tr>`
                for(var n=1;n<patentnum.length;n++ ){
                    c+=`<tr style="height: 120px;" id="a1">
                            <td>
                                <div>
                                    <div style="font-size: 18px;color: #000;">专利预期数量 </div>
                                    <div style="height: 2px;"></div>
                                    <div style="display: flex;justify-content: left;">
                                        <div style="font-size: 14px;color: #000;line-height: 30px;">专利类型</div>
                                        <div style="width: 1%"></div>
                                        <select style="width: 150px;height: 30px;"class="patentname">
                                              <option value="${patentnum[n].type_id}"  selected = "selected" style="display: none">${patentnum[n].content}</option>       
                                        </select>
                                        <div style="width: 143px"></div>
                                        <span>&nbsp;</span><div style="font-size: 14px;color: #000;line-height: 30px;">预期数量</div>
                                        <div style="width: 10px;"></div>
                                        <input type="text" placeholder="数量" style="width: 150px;height: 30px;padding-left: 10px;margin-top: 0;" class="patentnum" value="${patentnum[n].type_count}">
                                        <div style="width: 30px;height: 30px;border: 1px solid #999;line-height: 30px;text-align: center;border-left: none;color: #000;">项</div>
                                        <div style="width: 10px;"></div>
                            
                                    </div>
                                </div>
                            </td>
                        </tr>`
                }
                $("#t2").html(c);

            },
            error:function(err){
                console.log(err);
                alert("网络故障请检查");
            }
        });
    }
})
